package com.example.zbh.dsliao.ui.activty;

import android.content.Intent;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.easemob.chat.EMChatManager;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.utils.PreferenceHelper;


import java.util.Hashtable;


/**
 * Created by zbh on 15/12/4.
 */
public class TwoCodeActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout back;
    private ImageView two_code_image;
    private Button btn_create;
    private String CurrentUserName;
    private static int QR_WIDTH = 400;
    private static int QR_HEIGHT = 400;

    @Override
    protected int getLayOut() {
        return R.layout.activity_two_code;
    }

    @Override
    protected void initView() {
        back = findeView(R.id.back);
        two_code_image = findeView(R.id.two_code_image);
        btn_create = findeView(R.id.btn_create);
        String avater = PreferenceHelper.getInstance().getThreeCurrentUserAvatar();
        if (avater != null) {
            Glide.with(this)
                    .load(avater)
                    .placeholder(R.drawable.dl_default_avatar)
                    .error(R.drawable.dl_default_avatar)
                    .into(two_code_image);

        } else {
            Glide.with(this)
                    .load(R.drawable.dl_default_avatar)
                    .into(two_code_image);
        }




    }
//    private Camera.PreviewCallback  callback = new Camera.PreviewCallback() {
//        @Override
//        public void onPreviewFrame(byte[] data, Camera camera) {
//            int previewWidth = camera.getParameters().getPreviewSize().width;
//            int previewHeight = camera.getParameters().getPreviewSize().height;
//
//            PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(
//                    data, previewWidth, previewHeight, 0, 0, previewWidth,
//                    previewHeight, false);
//            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
//
//            QRCodeReader reader = new QRCodeReader();
//            try {
//
//                Result result = reader.decode(bitmap);
//                String text = result.getText();
//
//                Intent intent = new Intent();
//                intent.setData(Uri.parse(text));
//                setResult(RESULT_OK, intent);
//                finish();
//            } catch (Exception e) {
//                e.printStackTrace();
//                Toast.makeText(getApplicationContext(), "Not Found", Toast.LENGTH_SHORT).show();
//            }
//        }
//    };



    @Override
    protected void initData() {
        back.setOnClickListener(this);

        btn_create.setOnClickListener(this);
        CurrentUserName = EMChatManager.getInstance().getCurrentUser();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.btn_create:
//                createImage(CurrentUserName);
                break;

        }
    }

//    // 生成QR图
//    private void createImage(String content) {
//        try {
//            // 把输入的文本转为二维码
//            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
//            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//
//            BitMatrix bitMatrix = new QRCodeWriter().encode(content,
//                    BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
//
//            int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
//            for (int y = 0; y < QR_HEIGHT; y++) {
//                for (int x = 0; x < QR_WIDTH; x++) {
//                    if (bitMatrix.get(x, y)) {
//                        pixels[y * QR_WIDTH + x] = 0xff000000;
//                    } else {
//                        pixels[y * QR_WIDTH + x] = 0xffffffff;
//                    }
//
//                }
//            }
//
//            Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT,
//                    Bitmap.Config.ARGB_8888);
//
//            bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
//            if (bitmap != null) {
//                two_code_image.setImageBitmap(bitmap);
//            }
//
//        } catch (WriterException e) {
//            e.printStackTrace();
//        }
//    }

}
